package com.yd.other.remoteconfig;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;

import com.google.android.gms.tasks.OnCanceledListener;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseApp;
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings;
import com.yd.sdk.IUnityActivityLifecycleEvents;

public class RemoteConfigImp implements IUnityActivityLifecycleEvents {

    private static FirebaseRemoteConfig mFirebaseRemoteConfig;
    @Override
    public void onCreate(Activity activity, Bundle savedInstanceState) {

        FirebaseApp.initializeApp(activity);
        mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
        FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
                .setMinimumFetchIntervalInSeconds(3600)
                .build();
        mFirebaseRemoteConfig.setConfigSettingsAsync(configSettings);

        fetchAndActivate();
    }

    @Override
    public void onPause() {

    }

    @Override
    public void onResume() {

    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantRes) {

    }

    private void fetchAndActivate()
    {
        Log.e("RemoteConfigImp","fetchAndActivate");
        mFirebaseRemoteConfig.fetchAndActivate().addOnCompleteListener(new OnCompleteListener<Boolean>() {
            @Override
            public void onComplete(@NonNull Task<Boolean> task) {
                if (task.isSuccessful()) {
                    boolean updated = task.getResult();
                    Log.d("RemoteConfigImp", "Config params updated: " + updated);
//                    Toast.makeText(MainActivity.this, "Fetch and activate succeeded",
//                            Toast.LENGTH_SHORT).show();

                } else {
                    Log.d("RemoteConfigImp","Failed");
                }
            }
        });
    }

    public static String GetString(String key)
    {
        return mFirebaseRemoteConfig.getString(key);
    }


}
